/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mypomodoro;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
 *
 * @author lifsong
 */
public class TimerModel extends Model <TimerModelListener> {
    public final Integer ONE_POMODORO_SECONDS = (25*60);
    public final Integer BREAK_POMODORO_SECONDS = (5*60);
    public final Integer DELAY_MILLI_SEC = 10;    
    
    public final Property <Integer> timerLeftSec;
    
    protected Timer timer = null;
    
    public TimerModel(){
        this.timerLeftSec = new Property <Integer> (ONE_POMODORO_SECONDS);
    }

    public void decreaseLeftSec(){
        if(this.timerLeftSec.getValue() > 0){
            this.timerLeftSec.setValue( this.timerLeftSec.getValue() -1 );   
        }
    }
    
    public void createTimer(){
        if(timer == null){
            ActionListener runner = new ActionListener() {
                public void actionPerformed(ActionEvent evt){
                    decreaseLeftSec();
                }
            };
            timer = new Timer(DELAY_MILLI_SEC, runner);
        }
    }
    
    public void startTimer(){
        if(timer != null){
            timer.start();
        }
    }
    
    public void restartTimer(){
        if(timer != null){
            timer.restart();
        }
    }    

    public void stopTimer(){
        if(timer != null){
            timer.stop();
        }        
    }
}
